/*!
\copyright  Copyright (c) 2023 - 2024 Qualcomm Technologies International, Ltd.
            All Rights Reserved.
            Qualcomm Technologies International, Ltd. Confidential and Proprietary.
\file
\brief      The hfp profile audio params
*/

#include "hfp_profile_audio_params.h"

#include "mirror_profile.h"

#include <panic.h>
#include <logging.h>

static uint8 hfpProfile_GetPreStartDelay(void)
{
    uint8 pre_start_delay = 0;
    return pre_start_delay;
}

hfp_codec_mode_t HfpProfile_GetCodecMode(hfpInstanceTaskData * instance)
{ 
    hfp_codec_mode_t codec_mode = (instance->codec == hfp_wbs_codec_mask_msbc) ?
                                   hfp_codec_mode_wideband : hfp_codec_mode_narrowband;
#ifdef INCLUDE_SWB
    if(instance->qce_codec_mode_id != CODEC_MODE_ID_UNSUPPORTED)
    {
        switch (instance->qce_codec_mode_id)
        {
            case aptx_adaptive_64_2_EV3:
            case aptx_adaptive_64_2_EV3_QHS3:
            case aptx_adaptive_64_QHS3:
                codec_mode = hfp_codec_mode_super_wideband;
                break;

            case aptx_adaptive_128_QHS3:
                codec_mode = hfp_codec_mode_ultra_wideband;
                break;

            default:
                Panic();
                break;
        }
    }
#endif

#ifdef INCLUDE_SWB_LC3
    if (instance->codec == HF_PROFILE_WBS_LC3SWB_CODEC )
    {
        DEBUG_LOG("hfpProfile_GetCodecMode using HF_PROFILE_WBS_LC3SWB_CODEC codec");
        codec_mode = hfp_codec_mode_super_wideband_lc3;
    }

    DEBUG_LOG("AP: hfpProfile_GetCodecMode codec_mode %d", codec_mode);
#endif
    return codec_mode;
}

bool HfpProfile_GetConnectParameters(voice_source_t source, source_defined_params_t * source_params)
{
    hfpInstanceTaskData * instance = HfpProfileInstance_GetInstanceForSource(source);

    if (instance == NULL)
    {
        DEBUG_LOG_ERROR("hfpProfile_GetConnectParameters source enum:voice_source_t:%d has no instance", source);
        return FALSE;
    }

    PanicNull(source_params);

    voice_connect_parameters_t * voice_connect_params = (voice_connect_parameters_t *)PanicNull(malloc(sizeof(voice_connect_parameters_t)));
    memset(voice_connect_params, 0, sizeof(voice_connect_parameters_t));

    voice_connect_params->audio_sink = instance->sco_sink;
    voice_connect_params->codec_mode = HfpProfile_GetCodecMode(instance);
    voice_connect_params->mic_gain = appHfpGetMicGain(instance);
    voice_connect_params->wesco = instance->wesco;
    voice_connect_params->tesco = instance->tesco;
    voice_connect_params->volume = VoiceSources_CalculateOutputVolume(source);
    voice_connect_params->pre_start_delay = hfpProfile_GetPreStartDelay();
    voice_connect_params->synchronised_start = MirrorProfile_ShouldEscoAudioStartSynchronously(source);

    source_params->data = (void *)voice_connect_params;
    source_params->data_length = sizeof(voice_connect_parameters_t);

    return TRUE;
}

void HfpProfile_FreeConnectParameters(voice_source_t source, source_defined_params_t * source_params)
{
    UNUSED(source);

    PanicNull(source_params);
    PanicFalse(source_params->data_length == sizeof(voice_connect_parameters_t));
    if(source_params->data)
    {
        free(source_params->data);
        memset(source_params, 0, sizeof(*source_params));
    }
}

bool HfpProfile_GetDisconnectParameters(voice_source_t source, source_defined_params_t * source_params)
{
    UNUSED(source);

    PanicNull(source_params);
    memset(source_params, 0, sizeof(*source_params));

    return TRUE;
}

void HfpProfile_FreeDisconnectParameters(voice_source_t source, source_defined_params_t * source_params)
{
    UNUSED(source);

    PanicNull(source_params);
    if(source_params->data)
    {
        free(source_params->data);
        memset(source_params, 0, sizeof(*source_params));
    }
}
